package com.app.shortvideo.utils.jump

import android.text.TextUtils
import com.app.shortvideo.utils.jump.jumpers.BrowserJumper
import java.lang.reflect.InvocationTargetException

class JumperFactory {

    @Throws(
        NoSuchMethodException::class,
        IllegalAccessException::class,
        InvocationTargetException::class,
        InstantiationException::class
    )
    fun createJumper(action: String?): IJumper? {
        var jumper: IJumper? = null
        for (i in JUMPER_CLASSES.indices) {
            val clazz: Class<*> = JUMPER_CLASSES[i]
            if (TextUtils.isEmpty(action)) {
                continue
            }
            val constructor = clazz.getConstructor(String::class.java)
            val tempJumper: IJumper? = constructor.newInstance(action) as IJumper?
            if (tempJumper != null && tempJumper.isSupport()) {
                jumper = tempJumper
                break
            }
        }
        return jumper
    }

    companion object {
        /**
         * BrowserJumper必须放在数组的最后一个。
         * 因为其它jumpper也要处理类似于这样的scheme:http://www.pixbe.com/hot-detail
         */
        private val JUMPER_CLASSES = arrayOf<Class<*>>( //注意:　必须放在最后一个.
            BrowserJumper::class.java
        )
    }
}